package com.li.test.base;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class AppleDemo {
    volatile AtomicInteger jj = new AtomicInteger(0);

    public void add(){
        if (jj.get() <= 5&& jj.get()>=0) {
            jj.getAndIncrement();
            System.out.println("add值是："+jj.get() +".....当前线程："+Thread.currentThread().getName());
        }
    }
    public void  delete(){
        if(jj.get() <= 5 && jj.get()>=0){
            jj.getAndDecrement();
            System.out.println("delete值是："+jj.get() +".....当前线程："+Thread.currentThread().getName());

        }
    }

    public static void main(String[] args) {

        ThreadPoolExecutor th = new ThreadPoolExecutor(3,5,1, TimeUnit.SECONDS,new LinkedBlockingQueue<>());

        AppleDemo apple = new AppleDemo();
        for(int i=0;i<1000;i++){

        th.execute(()->{
            apple.delete();
        });
        th.execute(
                ()->{
                    apple.add();
                }
        );
        }
       /* for(int i=0;i<1000;i++){
            new Thread(()->{
                apple.delete();
            },"hhh:"+i).start();
        }

        for(int i=0;i<1000;i++){
            new Thread(()->{
                apple.add();
            },"eee"+i).start();
        }*/

    }
}
